package com.file_io;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class OOS {
    public static void saveStaffToFile1(Staff staff, File file){
        FileInputStream fis=null;
        ObjectInputStream ois=null;
        FileOutputStream fos=null;
        ObjectOutputStream oos=null;
        try{
            fis=new FileInputStream(file);
            ois=new ObjectInputStream(fis);
            List<Staff> list=(ArrayList<Staff>)ois.readObject();
            list.add(staff);
            fos=new FileOutputStream(file);
            oos=new ObjectOutputStream(fos);
            oos.writeObject(list);
            System.out.println("写入成功了");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                oos.close();
                fos.close();
                ois.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
    public static  List<Staff> readStaffFromFile1(File file){
        FileInputStream fis=null;
        ObjectInputStream ois=null;
        List<Staff> list=null;
        try{
            fis=new FileInputStream(file);
            ois=new ObjectInputStream(fis);
         list=(ArrayList<Staff>)ois.readObject();
            System.out.println("读取成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }




    public void saveStaffToFile(Staff staff, File file){
        StringBuffer stringBuffer=staff.toString(staff);
        FileOutputStream fos=null;
        OutputStreamWriter osw=null;
        BufferedWriter bdw=null;
        try {
         fos=new FileOutputStream(file);
         osw=new OutputStreamWriter(fos);
         bdw=new BufferedWriter(osw);
         bdw.write(stringBuffer.toString());
         bdw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bdw.close();
                osw.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public List<Staff> readStaffFromFile(File file){
        FileInputStream fis=null;
        InputStreamReader isr=null;
        BufferedReader br=null;
        List<Staff> list=new ArrayList<>();
        try {
            fis=new FileInputStream(file);
             isr=new InputStreamReader(fis);
            br=new BufferedReader(isr);
            String str;
            while ((str=br.readLine())!=null){
                int a=str.indexOf("/");
                int b=str.lastIndexOf("/");
                Staff staff1=new Staff();
                staff1.setName(str.substring(0,a));
                staff1.setAge(Integer.parseInt(str.substring(a+1,b)));
                staff1.setSalary(Double.parseDouble(str.substring(b+1)));
                list.add(staff1);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
            return list;
    }






    public static void main(String[] args) {
        Staff staff1=new Staff("tom",30,5000);
        Staff staff2=new Staff("jim",25,3000);
        ArrayList<Staff> arrayList=new ArrayList<>();
        arrayList.add(staff1);
        arrayList.add(staff2);

        FileOutputStream fos=null;
        ObjectOutputStream OOS=null;
        FileInputStream fis=null;
        ObjectInputStream ois=null;
        try {
          fos=new FileOutputStream("E:\\project-java65-parents\\homeWork\\io\\object1.txt");
           OOS=new ObjectOutputStream(fos);
           OOS.writeObject(arrayList);
           fis=new FileInputStream("E:\\project-java65-parents\\homeWork\\io\\object1.txt");
           ois=new ObjectInputStream(fis);
            ArrayList<Staff> arrayList1=(ArrayList<Staff>) ois.readObject();
            Iterator<Staff> iterator=arrayList1.iterator();
            while(iterator.hasNext()){
                Staff si=iterator.next();
                System.out.println("姓名："+si.getName()+"年龄："+si.getAge()+"工资："+si.getSalary());

            }
            for (Staff s:arrayList1) {
                System.out.println("姓名："+s.getName()+"年龄："+s.getAge()+"工资："+s.getSalary());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
        }

        Staff staff3 = new Staff("terry", 20, 5500);
        saveStaffToFile1(staff3,new File("E:\\project-java65-parents\\homeWork\\io\\object1.txt"));

        readStaffFromFile1(new File("E:\\project-java65-parents\\homeWork\\io\\object1.txt"));
    }
}
